﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace NuaTarDoneng
{
    public partial class BlogControl : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection cnn = new SqlConnection(ConfigurationManager.AppSettings["constr"]);
            SqlCommand cmd = cnn.CreateCommand();
            
           
            string id = Guid.NewGuid().ToString("N");
            
            cmd.CommandText = "insert into [blogSort] (blogSortID,blogSort) values('" + id + "','"+TextBox1.Text+"')";
            cnn.Open();
            if (cmd.ExecuteNonQuery() == 1)
            
            {
                Response.Redirect(Request.Url.ToString());
            }
            cnn.Close();
        }

        protected void GridView2_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            string id = e.Keys[0].ToString();
            SqlConnection cnn = new SqlConnection(ConfigurationManager.AppSettings["constr"]);
            SqlCommand cmd = cnn.CreateCommand();
            cnn.Open();
            if (id == "111")
            {
                cmd.CommandText = "insert into [blogSort] (blogSortID,blogSort) values('111','默认分类')";
                cmd.ExecuteNonQuery();
            }
            else
            {
                cmd.CommandText = "select count(*) from [blogs] where SortID='" + id + "'";
                
                int num = (int)cmd.ExecuteScalar();
                if (num > 0)
                {
                    cmd.CommandText = "update [blogs] set SortID='111' where SortID='" + id + "'";
                    cmd.ExecuteNonQuery();
                }
            }
            cnn.Close();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (e.Keys[0].ToString() == "LinkUp")
            {
                e.Cancel = true;
            }
        }

        protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            string id = e.Keys[0].ToString();
            SqlConnection cnn = new SqlConnection(ConfigurationManager.AppSettings["constr"]);
            SqlCommand cmd = cnn.CreateCommand();
            cmd.CommandText = "delete from [blogComment] where blogID='" + id + "'";
            cnn.Open();
            cmd.ExecuteNonQuery();
            cnn.Close();
        }

       
    }
}